package 锁;

//wait关键字和notify关键字的使用
public class Demo_6 {
    static Object A = new Object();
    static Object B = new Object();
    public static void main(String[] args) {
        Thread t1 = new Thread(()->{
            synchronized (A){
                try {
                    System.out.println("准备进入wait状态，等待notify唤醒");
                    A.wait();
                    System.out.println("被唤醒");
                } catch (InterruptedException e) {
                    System.out.println("被interrupt唤醒");
                }
            }
        });

        Thread t2 = new Thread(()->{
            synchronized (A){
                try {
                    //确保t1进入wait状态
                    Thread.sleep(1000);
                    System.out.println("准备唤醒A锁");
                    A.notify();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

//        Thread t3 = new Thread(()->{
//            try {
//                Thread.sleep(500);
//                t1.interrupt();
//            } catch (InterruptedException e) {
//                throw new RuntimeException(e);
//            }
//        });

        t1.start();
        t2.start();
//        t3.start();

    }
}
